using UnityEngine;
using System.Collections;

public class EnemyUpDownBehaviour : CharacterBehaviour
{
	public bool enable = true;
	
	[SerializeField]
	private Transform point1 = null, point2 = null;
	
	[SerializeField]
	private DetectorCollider detector = null;
	
	private Vector2 normalizedDirection = Vector2.zero;
	
	private Vector3 pointToGo = Vector3.zero;
	
	private float timeToAttack = 0f;
	
	private bool canMove = true;
	
	private GameObject targetToShot;
	
	protected override void Awake()
	{
		base.Awake();
		
		_transform = transform;
		
		point1.parent = null; point2.parent = null;
		
		if(enable)
		{
			StartCoroutine(WaitToStartMove());
		}
	}
	
	protected override void Update() 
	{
		if(enable && !onHit && hp > 0)
		{
			if(detector.IsFree)
			{
				timeToAttack = 0;
				if(targetToShot)
				{
					canMove = false;
					targetToShot = null;
				}
				
				if(base.animationUV.CanChangeAnimation)
				{ base.animationUV.Play("idle"); }
			}
			else if(ManagerGame.enableAction)
			{
				if(!targetToShot)
				{
					targetToShot = detector.GetAnyTarget;
				}
				
				if(targetToShot && base.animationUV.CanChangeAnimation)
				{
					pointToGo = targetToShot.transform.position + Vector3.up * 50;
					
					if(Mathf.Abs(base._transform.position.y - pointToGo.y) < 20)
					{
						timeToAttack -= Time.deltaTime;
						
						if(timeToAttack <= 0)
						{
							base.Shot(true);
							timeToAttack = 1f;
						}
						else
						{
							if(base.animationUV.CanChangeAnimation)
							{ base.animationUV.Play("idle"); }
						}
						
						canMove = false;
					}
					else
					{
						if(pointToGo.y > base.transform.position.y)
						{
							normalizedDirection = new Vector2(0, 1);
						}
						else
						{
							normalizedDirection = new Vector2(0, -1);
						}
						
						canMove = true;
					}
					
					if(targetToShot.transform.position.x > base._transform.position.x)
					{
						base._transform.localScale = new Vector3(1, _transform.localScale.y, _transform.localScale.z);
					}
					else
					{
						base._transform.localScale = new Vector3(-1, _transform.localScale.y, _transform.localScale.z);
					}
				}
			}
			
			if(canMove)
			{
				if(!base.footDetector.IsFree && normalizedDirection.y < 0)
				{
					normalizedDirection = new Vector2(normalizedDirection.x, 0);
				}
				
				if(Mathf.Abs(base._transform.position.y - pointToGo.y) > 20)
				{
					base.TryMove(normalizedDirection);
				}
				else
				{
					base.velocity = Vector2.zero;
				}
			}
			else
			{
				base.velocity = Vector2.zero;
			}
		}
		
		base.Update();
	}
	
	private IEnumerator WaitToStartMove()
	{
		yield return new WaitForSeconds(1f);
		
		if(detector.IsFree)
		{
			canMove = false;
		}
		
		StartCoroutine(WaitToChooseGoal());
	}
	
	private IEnumerator WaitToChooseGoal()
	{
		yield return new WaitForSeconds(1f);
		
		if(detector.IsFree)
		{
			if(pointToGo.Equals(point1.position))
			{
				pointToGo = point2.position;
			}
			else
			{
				pointToGo = point1.position;
			}
			
			canMove = true;
			
			normalizedDirection = new Vector2(0, pointToGo.y - _transform.position.y);
			
			normalizedDirection.Normalize();
		}
		
		StartCoroutine(WaitToStartMove());
	}
}
